import axios from 'axios'
// const host = 'http://shancloud.cn:8000'
const host = ''

export function post (url, params, options) {
  return axios({ method: 'post', url: host + url, data: params, headers: { 'X-CSRFToken': getCookie('csrftoken'), ...options } }).then(res => { return { error: '10', result: res.data } }).catch(err => { return { error: '21', result: err.response.data } })
}

export function get (url, params, options) {
  return axios({ method: 'get', url: host + url, params: params, headers: { 'X-CSRFToken': getCookie('csrftoken'), ...options } }).then(res => { return { error: '10', result: res.data } }).catch(err => {return { error: '21', result: err.response.data } })
}

export function put(url, params, options) {
  return axios({ method: 'put', url: host + url, data: params, headers: { 'X-CSRFToken': getCookie('csrftoken'), ...options} }).then(res => { return {error: '10', result: res.data} }).catch(err => {return {error: '21', result: err.response.data}})
}

export function Delete(url, params, options) {
  return axios({ method: 'delete', url: host + url, data: params, headers: { 'X-CSRFToken': getCookie('csrftoken'), ...options} }).then(res => { return {error: '10', result: res.data} }).catch(err => {return {error: '21', result: err.response.data}})
}

export function getCookie (name) {
  let reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
  let arr = document.cookie.match(reg)
  if (arr) { return unescape(arr[2]) } else { return null }
}

export function delCookie (name) {
  let exp = new Date()
  exp.setTime(exp.getTime() - 1)
  let cval = getCookie(name)
  if (cval != null) { document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString() }
}

export function isMobile () {
  return /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)
}
